use std::{env, fs::File};

use serde::{Serialize,Deserialize};
use anyhow::{Result, bail};

#[derive(Debug, Serialize, Deserialize)]
pub struct AppConfig{
    pub server: ServerConfig,

}


#[derive(Debug, Serialize, Deserialize)]
pub struct ServerConfig {
    pub host: String,
    pub port: u16,
}

impl AppConfig {
    pub fn load() -> Result<Self> {

        // read from app.yml or /etc/config/app.config or from env  CHAT_CONFIG

        let ret  =match (
            File::open("app.yml"),
            File::open("/etc/config/app.yml"),
            env::var("CHAT_CONFIG"),
        ){
            (Ok(reader),_,_) => serde_yaml::from_reader(reader),
            
            (_, Ok(reader),_) => serde_yaml::from_reader(reader),
            (_,_,Ok(path)) => serde_yaml::from_reader(File::open(path)?),
            _ => bail!("config file not found"),
        };

        Ok(ret?)

    }
}
